package exercise;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Exercise5 {
	public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 9};
        int[] arr2 = {3, 4, 5, 2};

        int[] result = mergeAndRemoveDuplicates(arr1, arr2);

        System.out.print("[");
        for (int i = 0; i < result.length; i++) {
            if (i > 0) {
                System.out.print(",");
            }
            System.out.print(result[i]);
        }
        System.out.println("]");
    }

    public static int[] mergeAndRemoveDuplicates(int[] arr1, int[] arr2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        Arrays.stream(arr1).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1));
        Arrays.stream(arr2).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1));

        return map.entrySet().stream()
                .filter(entry -> entry.getValue() == 1)
                .mapToInt(Entry::getKey)
                .toArray();
    }
}
